
package com.fact.actions;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.fact.dto.MakeDTO;
import com.fact.dto.SegmentDTO;
import com.fact.models.SegmentService;
import com.fact.utils.AppConstant;

public class Segment extends BaseAction implements SessionAware{
	
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	private ArrayList<SegmentDTO> segmentList;
	
	private String page=AppConstant.SEGMENT;
	private String pageTitle="FMtool - Segment List";

	SegmentDTO segment=new SegmentDTO();
	private String opType;
	SegmentService segmentService=new SegmentService();
	public String segmentList()
	{
		if(opType==null || (!opType.equalsIgnoreCase("save") && !opType.equalsIgnoreCase("delete") && !opType.equalsIgnoreCase("edit")))
			this.session.put("operation_message", null);
		
		segmentList= segmentService.getSegmentList();
		
		return SUCCESS;
	}
	public String newSegmentForm()
	{		
		return SUCCESS;
	}
	public String saveSegment()
	{ 
		
		if(segmentService.getSegment(segment.getSegmentName())!=null)
			addActionMessage("Segment Already Exist.");
		
		if(hasActionMessages())
			return  INPUT;


		if(segmentService.saveSegment(segment)==false)
				{
					addActionMessage("Error in Make Save Operation.");
					return INPUT;
				}
		else
				{
					this.session.put("operation_message", segment.getSegmentName()+" has successfully been added in Segment List.");
					return  SUCCESS;
				}

	}
	
	public String editSegment()
	{
		this.session.put("operation_message", null);
		SegmentDTO segmentTmp=segmentService.getSegment(segment.getSegmentId());
		boolean editFlag=false;
		if(segmentTmp==null)
		{
			editFlag=true;
		}
		else
		{
			if(segmentTmp.getSegmentId()!=segment.getSegmentId())
			{
				editFlag=false;
			}
			else
			{
				editFlag=true;
			}
		}
		if(editFlag==true)
		{
			if(segmentService.editSegment(segment)==false)
			{
				addActionMessage("Error in Segment Edit Operation.");
				return INPUT;
				
			}
			else
			{
				this.session.put("operation_message","Segment with Segment Name '"+ segment.getSegmentName()+"' has successfully been updated.");
				return  SUCCESS;
			}
		}
		else
		{
			addActionMessage("Update Falied. "+segment.getSegmentName()+" is already in the System.");
			return INPUT;
		}
	
	}
	public String deleteSegment()
	{
		if(segmentService.deleteSegment(segment.getSegmentId())==false)
		{
			addActionMessage("Error in Segment Delete Operation.");
			return INPUT;
			
		}
		else
		{
			this.session.put("operation_message","Segment with Segment Name '"+ segment.getSegmentName()+"' has successfully been deleted from Segment List.");
			return  SUCCESS;
		}
	
	}
	
	public ArrayList<SegmentDTO> getSegmentList() {
		return segmentList;
	}

	public void setSegmentList(ArrayList<SegmentDTO> segmentList) {
		this.segmentList = segmentList;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getPageTitle() {
		return pageTitle;
	}

	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}

	public SegmentDTO getSegment() {
		return segment;
	}

	public void setSegment(SegmentDTO segment) {
		this.segment = segment;
	}

	public String getOpType() {
		return opType;
	}

	public void setOpType(String opType) {
		this.opType = opType;
	}

	public void setSession(Map<String, Object> session) 
	{
		this.session = session;
	}
	public ServletContext getServletContext()
	{
		return ServletActionContext.getServletContext();
	}
}
